package org.example.admin.admin;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ShowAdminInfoView extends JFrame {
    private JTable table;
    private JButton buttonAlter;

    public ShowAdminInfoView() {
        setTitle("管理端个人信息查看页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("管理员id");
        model.addColumn("管理员名字");
        model.addColumn("管理员密码");

        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM air_admin");

            while (resultSet.next()) {
                int id = resultSet.getInt("admin_id");
                String username = resultSet.getString("admin_name");
                String password = resultSet.getString("password");
                Object[] rowData = {id, username, password};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAlter = new JButton("修改数据");
        buttonAlter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateAdminInfoView updateAdminInfoForm = new updateAdminInfoView();
                updateAdminInfoForm.setSize(400, 270);
                updateAdminInfoForm.setVisible(true);
            }
        });
        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAlter);
        add(Panel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            ShowAdminInfoView showAdminInfoViewForm = new ShowAdminInfoView();
//            showAdminInfoViewForm.setSize(400, 300);
//            showAdminInfoViewForm.setVisible(true);
//        });
    }
}